package com.axc.gallery2.edit;

import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Style;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.axc.gallery2.edit.effective.ToZoom;
import com.axc.gallery2.function.INormalPen;
import com.axc.gallery2.function.IOtherPenShape;
import com.axc.gallery2.function.IPenStyle;

public class ScrawlView extends View implements ISetScrawl, IPenShape,
		INormalPen, IOtherPenShape, IPenStyle {

	private Bitmap mBitmap;
	public static Bitmap mBitmapSave;
	Matrix matrix;
	private Canvas mCanvas;
	private Paint mBitmapPaint;// 画布的画笔
	private Paint mPaint;// 真实的画笔
	private int shape = -1;
	private Bitmap myshape;

	// 各种画笔标记 处理橡皮擦
	// 指示坐标 改为一支笔 或者一点
	Bitmap guiderBitmap = null;
	Canvas guiderCanvas = null;
	int guiderX, guidereY = -25;
	Paint guiderPaint;

	// 背景橡皮擦
	int eX, eY = 0;
	Paint bgPaint;
	Bitmap bgEarser;
	Bitmap bgEarserGuider;
	Canvas background;

	// 获取传递过来的图片数组（泡泡） //之后用事件做做
	int[] Paoimages;

	public void createEarser() {
		bgPaint = new Paint();
		bgPaint.setColor(Color.WHITE);
		bgEarserGuider = Bitmap.createBitmap(20, 20, Config.RGB_565);
		background = new Canvas(bgEarserGuider);
		background.drawColor(Color.BLUE);
	}

	public void drawEarser(Canvas canvas, Bitmap bitmap, int x, int y,
			Paint bgPaint) {
		canvas.drawBitmap(bitmap, x, y, bgPaint);
	}

	public void drawBgEarser(Canvas canvas, Bitmap source, int x, int y,
			Paint bgPaint) {
		bgEarser = Bitmap.createBitmap(source, x, y, bgEarserGuider.getWidth(),
				bgEarserGuider.getHeight());
		canvas.drawBitmap(bgEarser, x, y, bgPaint);
	}

	int w, h = 0;

	boolean canearser = false;
	boolean candraw = false;
	boolean haschangePen = false;
	boolean muli = false;
	boolean changeguider = false;

	int w1, h1, count1 = 0;

	public ScrawlView(Context context, int w, int h) {
		super(context);
		this.w = w;
		this.h = h;
		matrix = new Matrix();
		/**
		 * 获取图片
		 */
		mBitmap=BitmapFactory.decodeFile(EditActivity.imageFileNames);//ScrawlActivity.bmp;
		//mBitmap = ((BitmapDrawable) getResources().getDrawable(ScrawlActivity.imageID)).getBitmap();
		//mBitmap = ((BitmapDrawable) getResources().getDrawable(ScrawlActivity.imageID)).getBitmap();
		
		// 高度大于宽度
		mBitmap = new ToZoom().ProcessImage(mBitmap, w, h);
		mBitmapSave = new ToZoom().ProcessImage(mBitmap, w, h);
		// 高度小于宽度
		// bitmap=new ToZoom().ProcessImage(bitmap, w,h);
		// alteredBitmap = Bitmap.createBitmap(w - 40, h - 160, Config.RGB_565);
		// 保存一次一次绘制出来的图形
		mCanvas = new Canvas(mBitmapSave);

		mBitmapPaint = new Paint(Paint.DITHER_FLAG);
		mPaint = new Paint();
		mPaint.setColor(Color.GREEN);
		mPaint.setAntiAlias(true);
		mPaint.setStyle(Paint.Style.FILL);// 画笔 fill 填充划过的内容区
		// FILL_AND_STROKE内容区分了两层
		mPaint.setStrokeJoin(Paint.Join.ROUND);// 设置外边缘 round 圆 miter 尖角 bevel斜切
		mPaint.setStrokeCap(Paint.Cap.ROUND);// 形状 ROUND圆比较好看
		mPaint.setStrokeWidth(5);// 画笔宽度

		createGuider(Color.RED);

		createEarser();
		/*
		 * bgPaint = new Paint(); bgPaint.setColor(Color.WHITE); bgEarserGuider
		 * = Bitmap.createBitmap(20, 20, Config.RGB_565); background = new
		 * Canvas(bgEarserGuider); background.drawColor(Color.BLUE);
		 */

		candraw = true;
	}

	public void createGuider(int color) {
		guiderPaint = new Paint();
		guiderPaint.setStrokeWidth(3);
		guiderPaint.setColor(color);
		guiderBitmap = Bitmap.createBitmap(20, 20, Config.ARGB_4444);
		guiderCanvas = new Canvas(guiderBitmap);
		// 第一条线
		guiderCanvas.drawLine(0, guiderBitmap.getHeight() / 2, guiderBitmap
				.getWidth() / 4, guiderBitmap.getHeight() / 2, guiderPaint);
		// 第二条线
		guiderCanvas.drawLine(guiderBitmap.getWidth() * 3 / 4, guiderBitmap
				.getHeight() / 2, guiderBitmap.getWidth(), guiderBitmap
				.getHeight() / 2, guiderPaint);
		// 第三条线
		guiderCanvas.drawLine(guiderBitmap.getWidth() / 2, 0, guiderBitmap
				.getWidth() / 2, guiderBitmap.getHeight() / 4, guiderPaint);
		// 第四条线
		guiderCanvas.drawLine(guiderBitmap.getWidth() / 2, guiderBitmap
				.getHeight() * 3 / 4, guiderBitmap.getWidth() / 2, guiderBitmap
				.getHeight(), guiderPaint);
	}

	@Override
	public void setPenWidth(float width) {
		usePen();
		mPaint.setStrokeWidth(width);
		if (changeguider == true) {
			FilterStyle();
		}
	}

	@Override
	public void setPenColor(int color) {
		usePen();
		mPaint.setColor(color);
		if (changeguider == true) {
			FilterStyle();
		}
	}

	public void usePen() {
		canearser = false;
		candraw = true;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// canvas.drawColor(0xFFAAAAAA);
		// 将前面已经画过得显示出来
		canvas.drawBitmap(mBitmapSave, 0, 0, mBitmapPaint);
		/*
		 * if (mPath != null) { // 扩展 // canvas.drawRect(mX, mX, mY, mY,
		 * mPaint); // 实时的显示 canvas.drawPath(mPath, mPaint); }
		 */

		// 现在需要candraw=true 和 haschange=false
		if (candraw == true) {
			/*
			 * if (haschangePen == false) {//一按其他图形就change if(muli==true) {
			 * canvas.drawBitmap(guiderBitmap, guiderX - guiderBitmap.getWidth()
			 * / 2, guidereY - guiderBitmap.getHeight() / 2, mPaint); } else {
			 * canvas.drawBitmap(guiderBitmap, guiderX - guiderBitmap.getWidth()
			 * / 2, guidereY - guiderBitmap.getHeight() / 2, mPaint); } }
			 */
			/*if (muli == true) {
				canvas.drawBitmap(guiderBitmap, guiderX
						- guiderBitmap.getWidth() / 2, guidereY
						- guiderBitmap.getHeight() / 2, mPaint);
			}*/
			canvas.drawBitmap(guiderBitmap, guiderX - guiderBitmap.getWidth()
					/ 2, guidereY - guiderBitmap.getHeight() / 2, mPaint);
		}

		if (canearser) {
			// code
			try {
				drawBgEarser(canvas, mBitmap, eX - bgEarser.getWidth() / 2, eY
						- bgEarser.getWidth() / 2, bgPaint);
				canvas.drawBitmap(bgEarserGuider, eX
						- bgEarserGuider.getWidth() / 2, eY
						- bgEarserGuider.getWidth() / 2, guiderPaint);
				mCanvas.drawBitmap(bgEarser,
						eX - bgEarserGuider.getWidth() / 2, eY
								- bgEarserGuider.getWidth() / 2, guiderPaint);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	float downx = 0;
	float downy = 0;
	float upx = 0;
	float upy = 0;

	/**
	 * 绘图
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			if (canearser == true) {
				// code
				Log.w("earser", "xy");
				eX = (int) event.getX();
				eY = (int) event.getY();
			}

			if (candraw == true) {
				guiderX = (int) event.getX();
				guidereY = (int) event.getY();
				downx = event.getX();
				downy = event.getY();
				invalidate();
			}
			break;
		case MotionEvent.ACTION_MOVE:
			guiderX = (int) event.getX();
			guidereY = (int) event.getY();
			if (candraw == true) {
				if (haschangePen == true) {
					if (canearser == false) {
						if (muli == true) {
							if(count1>1)
							DrawMuliPao(Paoimages, w1, h1, count1);
							else {
								SingleImage(Paoimages, w1, h1, count1);
							}
						}
						mCanvas.drawBitmap(guiderBitmap, guiderX
								- guiderBitmap.getWidth() / 2, guidereY
								- guiderBitmap.getWidth() / 2, mPaint);
						invalidate();
					}
				} else {
					upx = event.getX();
					upy = event.getY();
					mCanvas.drawLine(downx, downy, upx, upy, mPaint);
					invalidate();
					// 保存移动过的位置
					downx = event.getX();
					downy = event.getY();
				}
			}
			if (canearser == true) {
				try {
					// code
					eX = (int) event.getX();
					eY = (int) event.getY(); // 画橡皮迹象
					// 我猜在创建对象时开始慢了
					bgEarser = Bitmap.createBitmap(mBitmap, eX
							- bgEarserGuider.getWidth() / 2, eY
							- bgEarserGuider.getWidth() / 2, bgEarserGuider
							.getWidth(), bgEarserGuider.getHeight());
					mCanvas.drawBitmap(bgEarser, eX - bgEarserGuider.getWidth()
							/ 2, eY - bgEarserGuider.getWidth() / 2,
							guiderPaint);
					invalidate();
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			break;
		case MotionEvent.ACTION_UP:

			if (canearser == true) {
				// code
				eX = (int) event.getX();
				eY = (int) event.getY();
			}
			if (candraw == true) {
				upx = event.getX();
				upy = event.getY();
				// mCanvas.drawLine(downx, downy, upx, upy, mPaint);
				guiderX = (int) event.getX();
				guidereY = (int) event.getY();
				invalidate();
			}
			break;
		}
		return true;
	}

	/**
	 * 重设
	 */
	@Override
	public void newCanvas() {
		usePen();
		// TODO Auto-generated method stub
		Bitmap bitmap = null;
		/**
		 * 获取图片
		 */
		bitmap=BitmapFactory.decodeFile(EditActivity.imageFileNames);
		/*bitmap = ((BitmapDrawable) getResources().getDrawable(
				ScrawlActivity.imageID)).getBitmap();*/
		
		
		// 高度大于宽度
		bitmap = new ToZoom().ProcessImage(bitmap, w, h);
		mCanvas.drawBitmap(bitmap, matrix, mPaint);
		guiderX=guidereY=-100;
		// 不知道为什么对象赋值好像变成了赋值而且不变的

		/*
		 * mBitmapSave=mBitmap; mCanvas.drawBitmap(mBitmapSave, matrix, mPaint);
		 */
		postInvalidate();
	}

	@Override
	public void Eraser() {

		canearser = true;
		candraw = false;
	}

	public void NoEraser() {
		canearser = false;
		candraw = true;
	}

	@Override
	public void CircleShape() {

		changeGuider();
		shape = 0;
		// 圆形
		guiderBitmap = Bitmap.createBitmap(20, 20, Config.ARGB_4444);
		guiderCanvas = new Canvas(guiderBitmap);
		guiderCanvas.drawCircle(guiderBitmap.getWidth() / 2, guiderBitmap
				.getWidth() / 2, guiderBitmap.getWidth() / 2, mPaint);
	}

	@Override
	public void rectangleShape() {

		changeGuider();
		shape = 1;
		// 正方形
		guiderBitmap = Bitmap.createBitmap(20, 20, Config.ARGB_4444);
		guiderCanvas = new Canvas(guiderBitmap);
		guiderCanvas.drawRect(new Rect(0, 0, guiderBitmap.getWidth() - 1,
				guiderBitmap.getHeight() - 1), mPaint);
	}

	@Override
	public void trigonShape() {

		changeGuider();
		shape = 2;
		guiderBitmap = Bitmap.createBitmap(20, 20, Config.ARGB_4444);
		// 三角形
		Path trianglePath = new Path();
		trianglePath.moveTo(guiderBitmap.getWidth() / 2, 0);
		trianglePath.lineTo(guiderBitmap.getWidth(),
				guiderBitmap.getHeight() - 1);
		trianglePath.lineTo(0, guiderBitmap.getHeight() - 1);
		trianglePath.close();

		guiderCanvas = new Canvas(guiderBitmap);
		guiderCanvas.drawPath(trianglePath, mPaint);

	}

	@Override
	public void setPenHollowStyle() {
		if (changeguider == true) {
			mPaint.setStyle(Style.STROKE);
			// 为的是刷新橡皮擦
			FilterStyle();
		}
	}

	@Override
	public void setPenfillStyle() {
		if (changeguider == true) {
			mPaint.setStyle(Style.FILL);
			FilterStyle();
		}
	}

	public void FilterStyle() {
		switch (shape) {
		case 0:
			CircleShape();
			break;
		case 1:
			rectangleShape();
			break;
		case 2:
			trigonShape();
			break;
		case 3:
			Normal();
			break;
		}
	}

	@Override
	public void Normal() {
		// TODO Auto-generated method stub
		createGuider(Color.RED);
	}

	@Override
	public void HasChangePen() {
		// TODO Auto-generated method stub
		haschangePen = true;
		NoEraser();
		muli = false;
		noChangeGuider();
	}

	@Override
	public void NoChangePen() {
		// TODO Auto-generated method stub
		haschangePen = false;
	}

	@Override
	public void LeaveShape(int imageID) {
		guiderBitmap = Bitmap.createBitmap(20, 20, Config.ARGB_4444);
		guiderCanvas = new Canvas(guiderBitmap);
		myshape = ((BitmapDrawable) getResources().getDrawable(imageID))
				.getBitmap();
		guiderCanvas.drawBitmap(myshape, 0, 0, mPaint);
	}

	@Override
	public void MilPao(int[] imageID, int w, int h, int count) {
		this.w1 = w;
		this.h1 = h;
		this.count1 = count;
		muli = true;
		Paoimages = imageID;
		if(count>1)
		DrawMuliPao(Paoimages, w, h, count);
		else
			DrawSingleImage(imageID, w, h, count);
		haschangePen = true;
		NoEraser();
	}

	public void DrawMuliPao(int[] imageID, int w, int h, int count) {
		guiderBitmap = Bitmap.createBitmap(w, h, Config.ARGB_4444);
		guiderCanvas = new Canvas(guiderBitmap);
		 
			for (int i = 0; i < count; i++) {
				Random r = new Random();
				float x = r.nextFloat() * w - 20;// 不是138是为了防止只画了半个圈
				float y = r.nextFloat() * w - 20;
				myshape = ((BitmapDrawable) getResources().getDrawable(
						imageID[i])).getBitmap();
				guiderCanvas.drawBitmap(myshape, x, y, mPaint);
			} 
			/*else {
			Random random = new Random();
			myshape = ((BitmapDrawable) getResources().getDrawable(
					imageID[random.nextInt(imageID.length)])).getBitmap();
			guiderCanvas.drawBitmap(myshape, 0, 0, mPaint);
		}*/
	}
	public void SingleImage(int[] imageID, int w, int h, int count){
		guiderBitmap = Bitmap.createBitmap(w, h, Config.ARGB_4444);
		guiderCanvas = new Canvas(guiderBitmap);
		Random random = new Random();
		myshape = ((BitmapDrawable) getResources().getDrawable(
				imageID[random.nextInt(imageID.length)])).getBitmap();
		guiderCanvas.drawBitmap(myshape, 0, 0, mPaint);
	}

	public void DrawSingleImage(int[] imageID, int w, int h, int count) {

	}

	public void changeGuider() {
		changeguider = true;
	}

	public void noChangeGuider() {
		changeguider = false;
	}
 

}
